﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Charting;
using System.Drawing;
using Telerik.Charting.Styles;

namespace BSCMFF.Web.Pages.Reporting
{
    /// <summary>
    /// Report for average competencies.
    /// </summary>
    /// <remarks></remarks>
    public partial class AverageCompetencies : BSCMFF.Core.MultiLanguagePage
    {
        /// <summary>
        /// border for poor performance
        /// </summary>
        private const int poor = 40;
        /// <summary>
        /// border for average performance
        /// </summary>
        private const int average = 90;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack && EvalSelection.PublicSelectedValue != 0)
            {
                FillChartCategories(EvalSelection.PublicSelectedValue);
                FillChartCompetencies(EvalSelection.PublicSelectedValue);
                PanelCharts.Visible = true;
            }
        }

        /// <summary>
        /// Fills the chart competencies.
        /// </summary>
        /// <param name="evalID">The eval ID.</param>
        /// <remarks></remarks>
        private void FillChartCompetencies(int evalID)
        {
            BSCEntities context = new BSCEntities();

            var data = (from ev in context.Evaluation_User_Question_Value
                        join eq in context.Evaluation_Question on ev.Evaluation_Question_FK equals eq.ID
                        join ec in context.Evaluation_Category on eq.Evaluation_Category_FK equals ec.ID
                        where ec.Evaluation_FK == evalID
                        group ev by new { ev.Evaluation_Question_FK } into g
                        join eqq in context.Evaluation_Question on g.Key.Evaluation_Question_FK equals eqq.ID
                        orderby eqq.ID descending
                        select new
                        {
                            questionID = g.Key.Evaluation_Question_FK,
                            question = eqq.Question,
                            //qlength = eqq.Question.Length,
                            perc = (int)(g.Average(ev => ev.Value - ev.Evaluation_Question.Evaluation_Category.Min) * 100 / (g.Average(ev => ev.Evaluation_Question.Evaluation_Category.Max - ev.Evaluation_Question.Evaluation_Category.Min)))
                            //perc = (g.Sum(qv => qv.Value) - g.Sum(ec => ec.Evaluation_Question.Evaluation_Category.Min)) * 100 / (g.Sum(ec => ec.Evaluation_Question.Evaluation_Category.Max) - g.Sum(ec => ec.Evaluation_Question.Evaluation_Category.Min))
                        });

            RadGridCompetencies.DataSource = data;
            RadGridCompetencies.DataBind();

            ChartSeries ch = new ChartSeries();
            ch.Name = "Competencies";
            ch.Type = ChartSeriesType.Bar;

            RadChartCompetencies.PlotArea.XAxis.Appearance.TextAppearance.TextProperties.Font = new Font("Arial", 10);
            RadChartCompetencies.PlotArea.XAxis.Appearance.TextAppearance.TextProperties.Color = Color.Black;
            RadChartCompetencies.PlotArea.XAxis.AutoScale = false;
            
            ChartSeries linePoor = new ChartSeries("LinePoor", ChartSeriesType.Line);
            linePoor.Appearance.ShowLabels = false;
            linePoor.Appearance.FillStyle.FillType = FillType.Solid;
            ChartSeries lineAverage = new ChartSeries("LineAverage", ChartSeriesType.Line);
            lineAverage.Appearance.ShowLabels = false;
            lineAverage.Appearance.FillStyle.FillType = FillType.Solid;
            foreach (var d in data)
            {

                linePoor.AddItem(poor);
                lineAverage.AddItem(average);

                RadChartCompetencies.PlotArea.XAxis.AddItem(d.question);
                ChartSeriesItem chi = new ChartSeriesItem();
                chi.YValue = (int)d.perc;

                if (chi.YValue < poor)
                {
                    chi.Appearance.FillStyle.MainColor = Color.Red;
                    chi.Appearance.FillStyle.FillType = FillType.Solid;
                }
                if (chi.YValue >= poor && chi.YValue < average)
                {
                    chi.Appearance.FillStyle.MainColor = Color.Yellow;
                    chi.Appearance.FillStyle.FillType = FillType.Solid;
                }
                if (chi.YValue >= average)
                {
                    chi.Appearance.FillStyle.MainColor = Color.Green;
                    chi.Appearance.FillStyle.FillType = FillType.Solid;
                }

                ch.AddItem(chi);
            }
                        
            RadChartCompetencies.AutoLayout = true;
            RadChartCompetencies.Series.Add(ch);


            RadChartCompetencies.AddChartSeries(linePoor);
            RadChartCompetencies.AddChartSeries(lineAverage);

            RadChartCompetencies.ChartTitle.TextBlock.Text = "Average Competencies";
        }

        /// <summary>
        /// Fills the chart categories.
        /// </summary>
        /// <param name="evalID">The eval ID.</param>
        /// <remarks></remarks>
        private void FillChartCategories(int evalID)
        {
            BSCEntities context = new BSCEntities();

            var data = (from ec in context.Evaluation_Category
                        join eq in context.Evaluation_Question on ec.ID equals eq.Evaluation_Category_FK
                        join ev in context.Evaluation_User_Question_Value on eq.ID equals ev.Evaluation_Question_FK
                        where ec.Evaluation_FK == evalID
                        group ev by new { ec.ID } into g
                        join ecc in context.Evaluation_Category on g.Key.ID equals ecc.ID
                        orderby ecc.ID descending
                        select new
                        {
                            categoryName = ecc.Name,
                            perc = //(g.Sum(x => x.Value - x.Evaluation_Question.Evaluation_Category.Min)
                                   (int)(g.Average(ev => ev.Value - ev.Evaluation_Question.Evaluation_Category.Min) * 100 / (g.Average(ev => ev.Evaluation_Question.Evaluation_Category.Max - ev.Evaluation_Question.Evaluation_Category.Min))) 
                        });

            RadGridCategories.DataSource = data;
            RadGridCategories.DataBind();

            ChartSeries ch = new ChartSeries();
            ch.Name = "Categories";
            ch.Type = ChartSeriesType.Bar;

            RadChartAverageCategories.PlotArea.XAxis.Appearance.TextAppearance.TextProperties.Font = new Font("Arial", 10);
            RadChartAverageCategories.PlotArea.XAxis.Appearance.TextAppearance.TextProperties.Color = Color.Black;
            RadChartAverageCategories.PlotArea.XAxis.AutoScale = false;

            foreach (var d in data)
            {
                RadChartAverageCategories.PlotArea.XAxis.AddItem(d.categoryName);
                ChartSeriesItem chi = new ChartSeriesItem();
                chi.YValue = d.perc;

                if (chi.YValue < poor)
                {
                    chi.Appearance.FillStyle.MainColor = Color.Red;
                    chi.Appearance.FillStyle.FillType = FillType.Solid;
                }
                if (chi.YValue >= poor && chi.YValue < average)
                {
                    chi.Appearance.FillStyle.MainColor = Color.Yellow;
                    chi.Appearance.FillStyle.FillType = FillType.Solid;
                }
                if (chi.YValue >= average)
                {
                    chi.Appearance.FillStyle.MainColor = Color.Green;
                    chi.Appearance.FillStyle.FillType = FillType.Solid;
                }

                ch.AddItem(chi);
            }
            
            RadChartAverageCategories.AutoLayout = true;
            RadChartAverageCategories.Series.Add(ch);

            RadChartAverageCategories.ChartTitle.TextBlock.Text = "Average Categories";
        }
    }
}